#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n, m;
int a[N];
struct S
{
	int v;
	int idx;
}s[N];
int find(int x, int y)
{
	int l = 0, r = n + 1;
	while (l + 1 < r) {
		int mid = (l + r) >> 1;
		if (s[mid].v <= x && mid <= y)l = mid;
		else r = mid;
	}
	return l;
}
int cmp(S a, S b)
{
	if (a.v != b.v)return a.v < b.v;
	return a.idx > b.idx;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			//scanf("%d", &a[i]);
			scanf("%d", &s[i].v);
			s[i].idx = i;
		}
		sort(s + 1, s + n + 1, cmp);
		for (int i = 1; i <= n; i++) {
			int x;
			scanf("%d", &x);
			int idx1 = find(x, i);
			//cout << "idx1==" << idx1 << endl;
			if (s[idx1].idx >= 1 && s[idx1].idx <= n) {
				if (s[idx1].idx <= i) cout << s[idx1].v << endl;
				else cout << -1 << endl;
			}
			else cout << -1 << endl;
		}
	}
	return 0;
}